package com.lbd.gulimall.order.service.impl;

import com.lbd.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.Query;

import com.lbd.gulimall.order.dao.OrderItemDao;
import com.lbd.gulimall.order.entity.OrderItemEntity;
import com.lbd.gulimall.order.service.OrderItemService;


@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    //RabbitHandler重载区分消息
    //只会接受hello-java-queue队列中 OrderItemEntity 格式的消息
    @RabbitHandler
    public void receiveMessage(Message message, OrderItemEntity entity, Channel channel) {
        //message：完整的消息内容 （消息头 + 消息体）
        //entity：Spring会自动根据生产者发送的数据格式进行转换为对应实体类
        //连接中的通道信息
    }

    //只会接受hello-java-queue队列中 OrderEntity 格式的消息
    @RabbitHandler
    public void receiveMessage(Message message, OrderEntity entity, Channel channel) {
        //message：完整的消息内容 （消息头 + 消息体）
        //entity：Spring会自动根据生产者发送的数据格式进行转换为对应实体类
        //连接中的通道信息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}